import { type BrowserWindow, screen, net } from 'electron'
import { ConfigType, IPResult } from './type'
// import path from 'path'
import md5 from 'md5'
import fs from 'fs'

const minWidth = 22, minHeight = 68
let width = minWidth
let height = minHeight
let snap = false
// 判断窗口竖直方向边缘
export function verticalEdge(win: BrowserWindow) {
    // 获取所有显示器
    const display = screen.getPrimaryDisplay()
    // 获取应用位置信息
    const size = win.getSize()
    const app = win.getPosition()
    // const bounds = win.getBounds()
    // 任务栏水平方向，做左右判断
    if (display.workArea.x == 0 && app[0] < 7 && !snap) {
        // 贴左边
        win.setBounds({
            x: 0, y: app[1], width: minWidth, height: minHeight
        }, true)
        width = size[0]
        height = size[1]
        snap = true
        win.webContents.send('edge', 'l') // left
        return 'l'
    }
    if (display.workArea.width + display.workArea.x == display.size.width && (display.size.width - app[0] - size[0]) < 7 && !snap) {
        // 贴右边
        win.setBounds({
            x: display.size.width - minWidth, y: app[1], width: minWidth, height: minHeight
        }, true)
        width = size[0]
        height = size[1]
        snap = true
        win.webContents.send('edge', 'r') // right
        return 'r'
    }

    if (snap) {
        win.setBounds({
            x: app[0], y: app[1], width, height
        }, true)
        snap = false
        win.webContents.send('edge', 'n') // none
        return 'n'
    }
    return false
}
// 判断窗口水平方向
export function horizontalEdge() { }

// 读取配置文件
export function getConfig(configPath: string) {
    // const configPath = path.join(exePath,)
    if (fs.existsSync(configPath)) {
        const config: ConfigType = JSON.parse(fs.readFileSync(configPath, "utf-8"))
        return config
    }
    return false
}

// 获取ip位置信息
export async function getIp(config: ConfigType | false) {
    if (!config) return
    const key = config.location.key
    const sn = config.location.sn
    const path = '/ws/location/v1/ip?key=' + key
    const sig = md5(path + sn)
    const res = await net.fetch(config.location.path + path + "&sig=" + sig)
    if (res.ok) {
        const body: IPResult = await res.json()
        if (body.message != 'Success') {
            console.log("定位信息获取失败", body)
            return
        }
        return body.result?.ad_info
    }
    return
}

// 实现窗口淡入淡出
export function fadeInWindow(window: BrowserWindow, type: 'in' | 'out' = 'in', duration = 300) {
    const steps = Math.ceil(duration / 20)
    let startOpacity = type == 'in' ? 0 : 1
    let opacity = startOpacity
    type == 'in' && window.show()

    const stepOpacity = 1 / steps
    const intervalId = setInterval(() => {
        opacity = type == 'in' ? stepOpacity + opacity : opacity - stepOpacity
        window.setOpacity(opacity)
        if (opacity >= 1 || opacity <= 0) {
            clearInterval(intervalId)
            type == 'out' && window.hide()
        }
    }, 20)
}

export { minWidth, minHeight }